package com.example.service.impl;

import com.example.common.Result;
import com.example.dto.PurQueryDto;
import com.example.entity.HnyyPurRebatePurHx;
import com.example.entity.HnyyPurRebatePurHxLines;
import com.example.mapper.HnyyPurRebatePurHxLinesMapper;
import com.example.mapper.HnyyPurRebatePurHxMapper;
import com.example.mapper.HnyyPurRebatePurListMapper;
import com.example.service.HnyyPurService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class HnyyPurServiceImp implements HnyyPurService {

    @Autowired
    private HnyyPurRebatePurListMapper hnyyPurRebatePurListMapper;
    @Autowired
    private HnyyPurRebatePurHxMapper hnyyPurRebatePurHxMapper;
    @Autowired
    private HnyyPurRebatePurHxLinesMapper hnyyPurRebatePurHxLinesMapper;

    @Override
    public PageInfo<PurQueryDto> getData(PurQueryDto purQueryDto) {
        // 开启分页查询
        PageHelper.startPage(purQueryDto.getPageNum(), purQueryDto.getPageSize());
        List<PurQueryDto> basicInfos = hnyyPurRebatePurListMapper.getData(purQueryDto);
        return PageInfo.of(basicInfos);
    }

    @Override
    public Result updatePurAmt(List<PurQueryDto> purQueryDtoList) {
        try {
            for(PurQueryDto purQueryDto : purQueryDtoList){
                purQueryDto.setOpFlag("ACCEPT");
            }
            hnyyPurRebatePurListMapper.updateList(purQueryDtoList);
            Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            Result.error();
        }
        return Result.ok();
    }

    public Result applyPur(String comPartyId, String user, List<HnyyPurRebatePurHxLines> hnyyPurRebatePurHxLines) {
        try {
            List<PurQueryDto> purQueryDtos = new ArrayList<>();
            HnyyPurRebatePurHx hnyyPurRebatePurHx = new HnyyPurRebatePurHx(comPartyId, user, ((HnyyPurRebatePurHxLines) hnyyPurRebatePurHxLines.get(0)).getVenderPartyId());
            hnyyPurRebatePurHx.setOpFlag("NEW");
            //插入主表
            hnyyPurRebatePurHxMapper.insert(hnyyPurRebatePurHx);

            for (HnyyPurRebatePurHxLines h : hnyyPurRebatePurHxLines) {
                h.setHnyyPurRebatePurHxId(hnyyPurRebatePurHx.getHnyyPurRebatePurHxId());
                PurQueryDto purQueryDto = new PurQueryDto();
                purQueryDto.setHnyyPurRebatePurListId(h.getHnyyPurRebatePurListId());
                purQueryDto.setTotalApplyAmt((h.getApplyAmt()));
                purQueryDtos.add(purQueryDto);
            }
            //插入明细
            hnyyPurRebatePurHxLinesMapper.insertList(hnyyPurRebatePurHxLines);
            //修改确权表申请核销金额
            hnyyPurRebatePurListMapper.updateList(purQueryDtos);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }

    @Override
    public PageInfo<HnyyPurRebatePurHx> getPurCGCheck(HnyyPurRebatePurHx purRebatePurHx) {
        PageHelper.startPage(purRebatePurHx.getPageNum(), purRebatePurHx.getPageSize());
        List<HnyyPurRebatePurHx> basicInfos = hnyyPurRebatePurHxMapper.getData(purRebatePurHx);
        return PageInfo.of(basicInfos);
    }

    @Override
    public PageInfo<HnyyPurRebatePurHxLines> getHnyyPurRebatePurHxLines(BigDecimal hnyyPurRebatePurHxId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        HnyyPurRebatePurHxLines hnyyPurRebatePurHxLines = new HnyyPurRebatePurHxLines();
        hnyyPurRebatePurHxLines.setHnyyPurRebatePurHxId(hnyyPurRebatePurHxId);
        List<HnyyPurRebatePurHxLines> basicInfos = hnyyPurRebatePurHxLinesMapper.query(hnyyPurRebatePurHxLines);
        return PageInfo.of(basicInfos);
    }

    @Override
    public Result buyerPass(List<HnyyPurRebatePurHx> purRebatePurHxes) {
        try {
            hnyyPurRebatePurHxMapper.update(purRebatePurHxes);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }
}
